package org.compactmvc.demo.model
{
	import flash.events.MouseEvent;
	
	import org.compactmvc.core.CompactMVC;
	import org.compactmvc.demo.ApplicationController;
	import org.compactmvc.demo.view.MainView;
	import org.compactmvc.demo.view.helpers.MainButton;
	import org.compactmvc.patterns.Controller;
	import org.compactmvc.patterns.Model;
	import org.compactmvc.viewpatterns.AbstractViewComponent;
	import org.compactmvc.viewpatterns.AbstractViewComponentModel;
	
	public final class MainViewModel extends AbstractViewComponentModel
	{
		public static const NAME:String = 'MainViewModel';
		
		[Inject]
		public var appModel:ApplicationModel;
		
		public function MainViewModel(name:String, controller:Controller, viewComponent:AbstractViewComponent)
		{
			super(name, controller, viewComponent);	
		}
		
		public function setupView():void
		{
			view.addEventListener(MouseEvent.CLICK, clickHandler, true, 0, true);
			view.create();
		}
		
		/**
		 * handles every view click event  
		 * @param e
		 * 
		 */		
		private function clickHandler(e:MouseEvent):void
		{
			if(e.target is MainButton)
			{
				switch(MainButton(e.target).viewName)
				{
					case "button1":
						sendNotification(ApplicationController.BUTTON_CLICKED, this);
						break;
					case "button2":
						//...
						break;
				}
				
			}
		}
		
		public function removeButton():void
		{
			view.removeChild(view.mainButton);
		}
		
		protected function get view():MainView
		{
			return viewComponent as MainView;
		}
		
		
	}
}